package com.qf.controller;

import com.qf.entity.Cart;
import com.qf.entity.Goods;
import com.qf.entity.ReqCart;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.utils.RedisUtils;
import com.qf.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/CartController")
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RedisUtils redisUtils;
    /**
     * 根据session中的id信息查询购物车
     * @return
     */
    @RequestMapping("/selAllCart")
    public List<ReqCart> selAllCart(HttpServletRequest request){
        System.out.println("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
        List<ReqCart> list  = new ArrayList<>();
//        int uId = (int)request.getSession().getAttribute("uId");
        int uId = (int)redisUtils.get("uId");
        List<Cart> cartList = cartService.selByUid(uId);
        for (Cart c:cartList ) {
            System.out.println("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"+c);
        }

//        int allPrice = 0;
        for (Cart c:cartList) {
            int g_id = c.getG_id();
            System.out.println("QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ============="+g_id);
            Goods goods = cartService.selByGoodsId(g_id);
            System.out.println("JJJJJJJJJJJJJJ============="+goods);
            ReqCart reqCart = new ReqCart();
            reqCart.setG_name(goods.getG_name());
            reqCart.setG_price(goods.getG_price());
            reqCart.setC_goodsnumber(c.getC_goodsnumber());
            reqCart.setC_goodssubtotal(c.getC_goodssubtotal());
//            allPrice += reqCart.getG_price()*reqCart.getC_goodsnumber();
            list.add(reqCart);
        }
        for (ReqCart l: list ) {
            System.out.println("YYYYYYYYYYYYYYYYYYYYYY======================="+l);
        }
        return list;
    }

    /**
     * 添加购物车
     * @return
     */
    @PostMapping("/addCart")
    public Msg addCart(@RequestBody Goods goods, HttpServletRequest request){
//        int uId = (int) request.getSession().getAttribute("uId");
        int uId = (int)redisUtils.get("uId");
        Cart cart = new Cart();
        cart.setU_id(uId);//用户的id
        cart.setG_id(goods.getG_id());//商品id
        Cart cart1 = cartService.selByUserIdAndGoodsId(cart.getU_id(), cart.getG_id());
        int flag = 0;
        if(cart1!=null){
            System.out.println("==============CCCCCCCCCCCCCCCCCCCCCCCCCC====================");
            System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAA=========================="+cart1);
            cart1.setC_goodsnumber(cart1.getC_goodsnumber()+1);
            cart1.setC_goodssubtotal(cart1.getC_goodssubtotal()+goods.getG_price()*1);
            System.out.println("VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV=========================="+cart1);
            cartService.updateByUserIdAndGoodsId(cart1);
            flag = 1;
        }else {
            cart.setC_goodsnumber(1);//单个商品的数量
            cart.setC_goodssubtotal(cart.getC_goodsnumber() * goods.getG_price());
            System.out.println("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK============================" + cart);
            int c = cartService.addCart(cart);
            flag = 1;
        }
        Msg msg = new Msg();
        if(flag==1) {
            msg.setMsg("ok");
        }else {
            msg.setMsg("no");
        }
        return msg;
    }
}
